Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nice_things/fs/basename.sh

basename

Since 0.3.0 · Source

import "{ basename }" from nice_things/fs/basename.sh

Synopsis
basename <pathname> [<suffix>]

Configuration

Description
Print <pathname> with any leading directory components removed. If specified, also remove a trailing <suffix>.

This is a pure sh implementation of the utility specified in POSIX.

Options

Operands

  • <pathname>: A path name.
  • <suffix>: An optional suffix.

Stdin

Stdout
The resulting filename is printed to stdout, trailed by a line-feed character.

Stderr

Exit status
0: Successful completion.

Abort

Usage examples

# Get name of current program
cmd=$(basename "$0")

# Print file name with extension removed
basename include/stdio.h .h # stdio